#pragma once
#include<iostream>
using namespace std;
namespace baka
{
	template<class K,class V>
	struct BSTreeNode
	{
		K _key;
		V _value;
		BSTreeNode* _right;
		BSTreeNode* _left;
		BSTreeNode(K key, V value)
			:_key(key)
			, _value(value)
			, _right(nullptr)
			,_left(nullptr)
		{}
	};

	template<class K,class V>
	class BSTree
	{
		typedef BSTreeNode<K,V> Node;
	public:
		BSTree() = default;
		BSTree(BSTree& tree)
		{
			Node* des = nullptr;
			_root = CreateTree(tree._root, des);
		}

		~BSTree()
		{
			_Delete(_root);
			_root = nullptr;
		}
		
		BSTree& operator=(BSTree tree)
		{
			_Delete(_root);
			swap(_root, tree._root);
			return *this;
		}
		bool Insert(const K& key, const V& value)
		{
			Node* newnode = new Node(key,value);
			if (_root == nullptr)
			{
				_root = newnode;
				return true;
			}
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (key > cur->_key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}
			if (key > parent->_key)
			{
				parent->_right = newnode;
				return true;
			}
			else if (key < parent->_key)
			{
				parent->_left = newnode;
				return true;
			}
			return false;
		}
		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}
		Node* Find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (key > cur->_key)
				{
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					cur = cur->_left;
				}
				else
				{
					return cur;
				}
			}
			return nullptr;
		}
		bool Erase(const K& key)
		{
			Node* cur = _root;
			Node* parent = cur;

			while (cur)
			{
				if (key > cur->_key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (key < cur->_key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					break;
				}
			}
			if (cur == nullptr)
			{
				return false;
			}
			
			if (cur->_left == nullptr)
			{
				if (parent == cur)
				{
					_root = cur->_right;
				}
				else
				{
					if (parent->_left == cur)
					{
						parent->_left = cur->_right;
					}
					else
					{
						parent->_right = cur->_right;
					}
				}
				delete cur;
				cur = nullptr;
				return true;
			}
			else if (cur->_right == nullptr)
			{
				if (parent == cur)
				{
					_root = cur->_left;
				}
				else
				{
					if (parent->_left == cur)
					{
						parent->_left = cur->_left;
					}
					else
					{
						parent->_right = cur->_left;
					}
				}
				delete cur;
				cur = nullptr;

				return true;
			}
			else
			{
				Node* rightside = cur->_right;
				Node* sizeparent = cur;
				while (rightside->_left)
				{
					sizeparent = rightside;
					rightside = rightside->_left;
				}
				cur->_key = rightside->_key;
				cur->_value = rightside->_value;
				if (sizeparent->_right == rightside)
				{
					sizeparent->_right = rightside->_right;
				}
				else
				{
					sizeparent->_left = rightside->_left;
				}
				delete rightside;
				rightside = nullptr;
				return true;
			}
			return false;
		}
		Node* CreateTree(Node* root, Node* des)
		{
			if (root == nullptr)
				return nullptr;
			des = new Node(root->_key, root->_value);
			des->_left = CreateTree(root->_left, des->_left);
			des->_right = CreateTree(root->_right, des->_right);
			return des;
		}
		void Delete()
		{
			_Delete(_root);
			_root = nullptr;
		}
		void _Delete(Node* root)
		{
			if (root == nullptr)
				return;
			_Delete(root->_left);
			_Delete(root->_right);
			delete root;
		}
	private:
		void _InOrder(Node* root)
		{
			if (root == nullptr)
				return;
			_InOrder(root->_left);
			cout << root->_key << " ";
			_InOrder(root->_right);
		}
		Node* _root=nullptr;
	};
}